<template>
  <div style="height: 100%">
    <el-container style="height: 100%">
      <!--      <el-aside :width="!menuCollapse ? '200px' : '64px'">-->
      <!--菜单栏-->
      <el-menu
        :default-active="activeMenu"
        :collapse="menuCollapse"
        class="el-menu-vertical-demo"
        background-color="#304156"
        text-color="#9fcbd9"
        active-text-color="#409eff"
        router
      >
        <el-menu-item index="/welcome">
          <i class="el-icon-s-home"></i>
          <span>首页</span>
        </el-menu-item>
        <!--一级子菜单-->
        <el-submenu
          :index="formatPath(null, item.name)"
          v-for="item in firstMenu"
          :key="item.id"
        >
          <!--一级菜单标题-->
          <template slot="title">
            <i :class="item.icon"></i>
            <span>{{ item.title }}</span>
          </template>
          <!--菜单分组-->
          <el-menu-item
            :index="formatPath(item.name, subItem.name)"
            v-for="subItem in secondMenus(item.id)"
            :key="subItem.id"
            class="menu-second"
          >
            <i :class="subItem.icon"></i>
            {{ subItem.title }}
          </el-menu-item>
        </el-submenu>
      </el-menu>
      <!--      </el-aside>-->
      <el-container>
        <el-header>
          <div class="left-header">
            <i
              :class="!menuCollapse ? 'el-icon-s-fold' : 'el-icon-s-unfold'"
              @click="menuCollapse = !menuCollapse"
            ></i>
<!--            <el-breadcrumb separator-class="el-icon-arrow-right">-->
<!--              <el-breadcrumb-item :to="{ path: '/welcome' }"-->
<!--                >首页</el-breadcrumb-item-->
<!--              >-->
<!--            </el-breadcrumb>-->
          </div>
          <div>
            <el-dropdown @command="handleCommand">
              <span class="el-dropdown-link">
                <el-avatar> {{ loginUser.icon }} </el-avatar>
                {{ loginUser.username }}
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
<!--                <el-dropdown-item command="change">修改密码</el-dropdown-item>-->
                <el-dropdown-item command="quit">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-header>
        <el-main>
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { api_logout } from '../api/admin'
import { api_info } from '../api/admin'
import { api_refreshToken } from '../api/admin'

export default {
  data() {
    return {
      menuCollapse: false,
      activeMenu: '',
      loginUser: {
        username: '',
        icon: '',
        role: '',
      },
      menus: [],
      levelList: null,
    }
  },
  created: function () {
    //定时执行任务12h/次
    setInterval(this.refreshToken, 12 * 60 * 60 * 1000)
  },
  mounted() {
    this.activeMenu = window.sessionStorage.getItem('activeMenu')
    if (!this.activeMenu) {
      this.activeMenu = '/welcome'
    }
    this.getInfo()
  },
  computed: {
    firstMenu() {
      //取出一级菜单
      return this.menus.filter((item) => item.parentId === 0)
    },
    secondMenus() {
      return (parendId) =>
        this.menus.filter((item) => item.parentId === parendId)
    },
  },
  watch: {
    $route(to) {
      this.selectMenu(to.path)
    },
  },
  methods: {
    handleCommand(command) {
      if (command === 'quit') {
        this.handleLogout()
      }
    },
    async handleLogout() {
      const { data: res } = await api_logout()
      if (!res.success) {
        //err
        this.$message.error(res.msg)
        return
      }
      //success
      //清除session
      window.sessionStorage.clear()
      //跳转到登录页面
      await this.$router.replace('/login')
      this.$message({
        message: res.msg,
        type: 'success',
      })
    },
    //刷新token
    async refreshToken() {
      let token = window.sessionStorage.getItem('token')
      const { data: res } = await api_refreshToken(token)
      if (res) {
        token = res.data.tokenHead + res.data.token
        window.sessionStorage.setItem('token', token)
      }
    },
    selectMenu(index) {
      //菜单部分
      this.activeMenu = index
      window.sessionStorage.setItem('activeMenu', index)
    },
    async getInfo() {
      let userId = window.sessionStorage.getItem('userId')
      const { data: res } = await api_info(userId)
      if (res) {
        this.loginUser.username = res.data.userName
        this.loginUser.icon = res.data.icon
        this.menus = res.data.menus
      }
    },
    formatPath(folder, name) {
      name = '/' + name
      if (folder) {
        name = '/' + folder + name
      }
      return name
    },
  },
}
</script>

<style scoped>
.el-header {
  background-color: #ffffff;
  color: #333;
  text-align: center;
  line-height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: #d3dce6 solid 1px;
}

.left-header {
  cursor: pointer;
  display: flex;
  flex-direction: row;
}
.el-breadcrumb {
  font-size: 20px;
  margin-top: 5px;
  margin-left: 10px;
}

.left-header > i {
  font-size: 30px;
}

.el-avatar {
  position: relative;
  top: 14px;
  margin-right: 5px;
}

.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}

.el-aside {
  background-color: #304156;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}

.el-menu {
  border: none;
}

.el-menu > .el-menu-item,
.el-submenu {
  text-align: left;
}

.menu-second {
  background-color: #243547 !important;
}

.menu-second:hover {
  background-color: #1d2f44 !important;
}

.el-main {
  background-color: #f9f9f9;
  color: #333;
  text-align: left;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
</style>
